from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest,time

class Login(unittest.TestCase):
    def setUp(self):
#        self.driver = webdriver.Firefox()
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com"
        self.verificationErrors = []
    
    def test_login(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_xpath("//a[@name='tj_login']").click()
        driver.find_element_by_xpath("//div[contains(concat(' ', @class, ' '), ' bd_dialog_handle ')]")
        driver.switch_to_frame("login_iframe")

        driver.find_element_by_id("pass_login_username_0").clear()
        driver.find_element_by_id("pass_login_username_0").send_keys("zhengchunxi@foxmail.com")
        driver.find_element_by_id("pass_login_password_0").clear()
        driver.find_element_by_id("pass_login_password_0").send_keys("testMiaozhen")
        driver.find_element_by_id("pass_login_rem_0").click()
        driver.find_element_by_id("pass_login_input_submit_0").click() 
        time.sleep(5)
        driver.quit()         
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException: return False
        return True
    
    def tearDown(self):
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
